package ca.camosun.proximity;


public class Room {
	
	private DeviceTracker peopleIKnow;
	private DeviceTracker unknownPeople;
	public DeviceTracker peopleInRoom;
	private DeviceTracker recentDepartures;
	private DeviceTracker recentArrivals;
	private DeviceTracker peopleToKeepAway;
	private DeviceTracker peopleToKeepClose;
	private static Room instance = null;
	
	public static Room getInstance() {
		if (instance == null) {
			instance = new Room();
		}
		return instance;
	}

	protected Room (){
		peopleIKnow = new DeviceTracker("Saved");
		unknownPeople = new DeviceTracker("Unknown");
		peopleInRoom = new DeviceTracker("InRoom");
		recentDepartures = new DeviceTracker("Recently Left range");
		recentArrivals = new DeviceTracker("Recently got in range");
		peopleToKeepAway = new DeviceTracker("People to Keep away");
		peopleToKeepClose = new DeviceTracker("People to keep close");
	}

	public boolean contains(SavedPerson who){
		return peopleInRoom.contains(who);
	}
	
	public void foundADevice(SavedPerson aDevice) {
		if (peopleIKnow.getDevicePosition(aDevice)==-1 && unknownPeople.getDevicePosition(aDevice)==-1) {
			unknownPeople.addDevice(aDevice);
		}
		peopleInRoom.addDevice(aDevice);
		recentArrivals.addDevice(aDevice);
		if (recentDepartures.contains(aDevice)) {
			recentDepartures.removeDevice(aDevice);
		}
	}
	
	public void recognizeDevice(SavedPerson aDevice,  String aName) {
		unknownPeople.removeDevice(aDevice);
		aDevice.setName(aName);
		peopleIKnow.addDevice(aDevice);
	}
	
	public void removeDevicefromKnown(SavedPerson aDevice) {
		peopleIKnow.removeDevice(aDevice);
		unknownPeople.addDevice(aDevice);
		aDevice.setName("");
	}
	
	public void userLeavesRoom(SavedPerson aDevice){
		recentDepartures.addDevice(aDevice);
		recentArrivals.removeDevice(aDevice);
		peopleInRoom.removeDevice(aDevice);
	}
	
	public void keepPersonClose(SavedPerson aDevice){
		peopleToKeepClose.addDevice(aDevice);
		if (peopleToKeepAway.contains(aDevice)) {
			peopleToKeepAway.removeDevice(aDevice);
		}
	}
	
	public void keepPersonAway(SavedPerson aDevice){
		peopleToKeepAway.addDevice(aDevice);
		if (peopleToKeepClose.contains(aDevice)) {
			peopleToKeepClose.removeDevice(aDevice);
		}
	}
	
	public int howManyPeopleAreInRoom(){
		return peopleInRoom.deviceCount();
	}

	public DeviceTracker getDepartures() {
		return recentDepartures;
	}
	
	public DeviceTracker getArrivals() {
		return recentArrivals;
	} 

	public DeviceTracker getPeopleToKeepClose() {
		return peopleToKeepClose;
	}
	
	public DeviceTracker getPeopleToKeepAway() {
		return peopleToKeepAway;
	} 
	
	public boolean isPersonStillNear(SavedPerson aPerson) {
		return peopleInRoom.contains(aPerson);
	}
	
	public boolean isPersonStillAway(SavedPerson aPerson) {
		if (peopleInRoom.contains(aPerson)) {
			return false;
		}
		return true;
	}
	
	public String[] listOfDevicesInRoom() {
		return peopleInRoom.listOfDevices();
	}
	
	public String[] listOfKnownDevices() {
		return peopleIKnow.listOfDevices();
	}
	
	public String[] listOfUnknownDevices() {
		return unknownPeople.listOfDevices();
	}

}
